package com.sangmo.fts.user.service;

import com.sangmo.boot.framework.common.errors.ServiceException;
import com.sangmo.boot.framework.common.errors.StandardErrors;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.boot.framework.common.utils.PasswordUtils;
import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.teacher.interfaces.ScoreSettingCreation;
import com.sangmo.fts.teacher.service.ScoreSettingService;
import com.sangmo.fts.user.interfaces.AccountCreation;
import com.sangmo.fts.user.model.Role;
import com.sangmo.fts.user.model.User;
import com.sangmo.fts.user.repo.PermissionRepo;
import com.sangmo.fts.user.repo.RoleRepo;
import com.sangmo.fts.user.repo.UserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

@Service
@Transactional
public class UserEntityService extends BaseViewService<User> {

    @Autowired
    private UserRepo userRepo;

    @Autowired
    private PermissionRepo permissionRepo;

    @Autowired
    private RoleRepo roleRepo;

    @Autowired
    private AccountService accountService;

    @Autowired
    private ScoreSettingService scoreSettingService;

    private static final BigDecimal initialFunds = new BigDecimal("1000000");

    @Override
    protected IDataViewRepo<User> getRepo() {
        return userRepo;
    }

    public User findByLoginName(String loginName) {
        return userRepo.findOneByLoginName(loginName);
    }

//    protected Collection<Role> findRoles(Long id) {
//        Collection<Role> roles = roleRepo.findByUserId(id);
//        return roles;
//    }


    protected Collection<String> findPermissions(Long id) {
        Collection<String> perms = permissionRepo.findPermissionsByUserId(id);
        Set<String> permSet = new HashSet<>();
        for (String p : perms) {
            String[] ps = p.split(",", -1);
            for (String ps1 : ps) {
                permSet.add(ps1);
            }
        }
        return permSet;
    }

    public void validatePassword(Long id, String password) {
        User auth = userRepo.findOne(id);
        String encripted = PasswordUtils.encript(password);

        if (!encripted.equals(auth.getPassword())) {
            throw new ServiceException(StandardErrors.CLIENT_ERROR.getStatus(), "error.account.password");
        }
    }

    @Override
    protected void afterCreated(User entity, Object bo) {
        super.afterCreated(entity, bo);
        AccountCreation creation = new AccountCreation();
        //暂时学生才创建资产账户
        if ("4".equals(entity.getExRoleId())) {
            creation.setUserId(entity.getId());
            creation.setInitialFunds(initialFunds);
            creation.setDisposableFunds(initialFunds);
            creation.setInventoryPrice(BigDecimal.ZERO);
            accountService.create(creation);
        }
        // 老师用户 创建一份默认的成绩设置
        else if ("3".equals(entity.getExRoleId())) {
            ScoreSettingCreation settingCreation = ScoreSettingCreation.defaultOne();
            settingCreation.setUserId(entity.getId());
            scoreSettingService.create(settingCreation);
        }

    }

    public static void main(String[] args) {
       System.out.print(PasswordUtils.encript("111111"));
    }
}
